package edu.caltech.android.database;

import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

public class RingBuffer<T> {

	//private final int capacity;
	
	private final ArrayBlockingQueue<T> queue;
	
	public RingBuffer(int capacity){
		//this.capacity = capacity;
		queue = new ArrayBlockingQueue<T>(capacity);
	}
	
	public synchronized boolean isEmpty(){
		return queue.isEmpty();
	}
	
	public synchronized void clear(){
		queue.clear();
	}
	
	public synchronized void add(T e){
		if (queue.offer(e) == false) {
			// if queue is full, pop then add
			queue.remove();
			queue.offer(e);
		}
	}
	
//	public synchronized T[] toArray(){
//		return (T[]) queue.toArray();
//	}
//	
	public synchronized ArrayList<T> getContents(){
		ArrayList<T> contents = new ArrayList<T>();
		for (T t : queue) {
			contents.add(t);
		}
		return contents;
	}
	
}
